package mhys.View.Admin;

import mhys.Dao.ClassesDao;
import mhys.Dao.TeaDao;
import mhys.Entity.Classes;
import mhys.Entity.Teacher;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class addTeaJpanel extends JPanel {
    DefaultTableModel model;
    JTable jTable;
    ClassesDao classesDao = new ClassesDao();
    TeaDao teadao = new TeaDao();
    public addTeaJpanel(){
        this.setBackground(Color.WHITE);
        this.setLayout(new BorderLayout(10,10));
        JPanel jPanel = new JPanel();
        jPanel.setLayout(null);

        JLabel nameJLabel = new JLabel("教师姓名：");
        nameJLabel.setBounds(10,60,80,25);
        final JTextField nameTextField = new JTextField();
        nameTextField.setBounds(90,60,150,25);

        JLabel number = new JLabel("工号：");
        number.setBounds(10,100,80,25);
        final JTextField numberTextField = new JTextField();
        numberTextField.setBounds(90,100,150,25);

        JLabel sexJLabel = new JLabel("性别：");
        sexJLabel.setBounds(10, 140, 80, 25);
        String[] itemSex = {"男","女"};
        final JComboBox<String> jComboBox = new JComboBox<String>(itemSex);
        jComboBox.setBounds(90, 140, 150, 25);

        JLabel classJLabel = new JLabel("班级：");
        classJLabel.setBounds(10, 180, 80, 25);


        final JComboBox<String> classBox = new JComboBox<String>();
        List<Classes> classes = classesDao.getClasses();
        //去重
        Set<String> set = new HashSet<String>();
        for (Classes c: classes) {
            String s = c.getC_name();
            if (set.add(s)) {
                classBox.addItem(s);
            }
        }
        classBox.setBounds(90, 180, 150, 25);

        JLabel phoneJLabel = new JLabel("手机号：");
        phoneJLabel.setBounds(10, 220, 80, 25);
        final JTextField phoneTextField = new JTextField();
        phoneTextField.setBounds(90, 220, 150, 25);

        JButton addButton = new JButton("添加");
        addButton.setBounds(100, 260, 80, 25);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String t_number = numberTextField.getText();
                String t_name = nameTextField.getText();
                String sex = (String)jComboBox.getSelectedItem();
                String classes = (String)classBox.getSelectedItem();
                String phone = phoneTextField.getText();
                if (t_number.isEmpty() || t_name.isEmpty() || sex.isEmpty()||classes.isEmpty()||phone.isEmpty()){
                    JOptionPane.showMessageDialog(null, "不能为空");
                }else {
                    Teacher teacher = new Teacher(null,t_name,t_number,sex,classes,phone);
                    int addTea = teadao.addTea(teacher);
                    if (addTea == 1){
                        List<Teacher> teachers = teadao.getTeachers();
                        JOptionPane.showMessageDialog(null, "添加成功！");
                        numberTextField.setText("");
                        nameTextField.setText("");
                        jComboBox.setSelectedItem("男");
                        classBox.setSelectedItem("");
                        phoneTextField.setText("");
                    }
                }

            }
        });


        jPanel.add(nameJLabel);
        jPanel.add(nameTextField);
        jPanel.add(number);
        jPanel.add(numberTextField);
        jPanel.add(sexJLabel);
        jPanel.add(jComboBox);
        jPanel.add(classJLabel);
        jPanel.add(classBox);
        jPanel.add(phoneJLabel);
        jPanel.add(phoneTextField);
        jPanel.add(addButton);
        jPanel.setBackground(Color.white);
        this.add(jPanel,BorderLayout.CENTER);
    }
}
